var showDataDetail;
var $dataChart;

require(['grid', 'topBar', 'layout', 'date'], function () {

    var postData;

    $('#top').topBar({
        left: [{
            type: 'filter-more',
            dateType:'custom',
            search: function () {
                if (getPostData()) {
                    $('#grid').grid('setGridParam', {
                        page: 1,
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }
            ]
        }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });

    $('body').layout();

    function exportFile(suffix) {
        if (getPostData()) {
            if ($('#grid').getGridParam("reccount") > 0) {
                var url = "/chargingDetail/exportChargingDetail" + suffix + ".htm";
                $.exportFile(url, postData);
            }
            else {
                $.messager.alert("提示", "请先查询数据！", "info");
            }
        }
    }

    //获取左侧选择的节点信息
    function getPostData() {
        var rcx = commonFun.getTreeCheckNodeToArgs();
        if (rcx) {
            postData = $.extend(rcx, $("#top").parent().getValues());
        }
        return rcx;
    }

    $('#grid').grid({
        url: "/chargingDetail/searchChargingDetail.htm",
        multiselect: false,
        colModel: [
            {name: "lineId", label: "lineId", hidden: true},
            {name: "carId", label: "carId", hidden: true},
            {name: "licPltNo", label: "车牌号", width: "80"},
            {name: "lineName", label: "运营线路", width: "80"},
            {name: "organName", label: "所属机构", width: "80"},
            {name: "carType", label: "车辆型号", width: "110"},
            {name: "bteryMaterialType", label: "电池材料类型", width: "110"},
            {name: "chargeBeginTime", label: "充电开始时间", width: "110"},
            {name: "chargeBeginSocValue", label: "充电前SOC值(%)", width: "110", align: "right"},
            {name: "chargeEndTime", label: "充电结束时间", width: "110"},
            {name: "chargeEndSocValue", label: "充电后SOC值(%)", width: "110", align: "right"},
            {name: "runTime", label: "单次充电时长", width: "110"},
            {name: "chargeingTotalMile", label: "总行驶里程", width: "110"},
            {name: "chargeBefEltValue", label: "充电前电度表值(kwh)", width: "110", align: "right"},
            {name: "chargeAftEltValue", label: "充电后电度表值(kwh)", width: "110", align: "right"},
            {name: "chargeNum", label: "充电量(kwh)", width: "110", align: "right"},
            {
                name: "detail", label: "操作", width: "120", align: "center",
                formatter: 'operArea',
                formatoptions: [
                    {
                        text: '详细信息',
                        css:'operate-scan',
                        click: function (data) {
                            showDataDetail(data);
                        }
                    }
                ]
            }]
    });

    //显示详情
    showDataDetail = function (data) {
        if (!$dataChart) {
            $dataChart = $('<div>').dialog({
                title: '详细信息',
                url: './mainte/report/viewChargingDetail.htm',
                height: 550,
                width: 720,
                buttons: [
                    {
                        text: '关闭',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        $dataChart.dialog('open');
        $dataChart.dialog('exec', 'showDetail', data);
    }

})
